public class Text4 {
    //二分查找
    public static int binarySearach(int[] a, int key) {
        int left = 0;
        int right = a.length - 1;

        while(left <= right) {
            int mid = (left + right) / 2;
            if(key > a[mid]) {
                left = mid + 1;
            } else if(key < a[mid]) {
                right = mid - 1;
            }else {
                return mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] a = {1,2,3,4,5};
        int key = 4;
        System.out.println(binarySearach(a, key));
    }

}
